<?php

namespace app\common\model;

use think\Model;


class UserThird extends Model
{

    

    

    // 表名
    protected $name = 'user_third';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'logintime_text',
        'expiretime_text',
        'authorization_status_text'
    ];
    

    
    public function getAuthorizationStatusList()
    {
        return ['1' => __('Authorization_status 1'), '2' => __('Authorization_status 2'), '3' => __('Authorization_status 3')];
    }


    public function getLogintimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['logintime']) ? $data['logintime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getExpiretimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['expiretime']) ? $data['expiretime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getAuthorizationStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['authorization_status']) ? $data['authorization_status'] : '');
        $list = $this->getAuthorizationStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setLogintimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setExpiretimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function decryptUserInfo($post){
        $wxmini=new \app\common\library\Wxmini();
        $return=$wxmini->decryptData($post['session_key'], $post['iv'], $post['encryptedData']);
        return [
            'status'=>true,
            'data'=>[
                'nickname'=>isset($return['nickName'])?$return['nickName']:'',
                'avatar'=>isset($return['avatarUrl'])?$return['avatarUrl']:'',
                'authorization_status'=>2,
            ]
        ];
    }
    public function decryptPhoneNumber($post){
        $wxmini=new \app\common\library\Wxmini();
        $return=$wxmini->decryptData($post['session_key'], $post['iv'], $post['encryptedData']);
        return [
            'status'=>true,
            'data'=>[
                'mobile'=>isset($return['phoneNumber'])?$return['phoneNumber']:'',
            ]
        ];
    }
}
